/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.controller.articleAdmin;

import com.mxg.blog.common.CqLogger;
import com.mxg.blog.common.RegularExpression;
import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/5/10 12:22
 * 自动生成一张带有文章标题的图片 并发送到前端
 */
@Controller
public class ArticleCoverImgController {
    CqLogger logger = CqLogger.getInstance();

    @Value("${prop.upload-folder}")
    private String UPLOAD_FOLDER;

    @GetMapping("api/coverImg")
    public void getCoverImg(@RequestParam String text, HttpServletResponse response) throws Exception{
        // 返回的hash表
        // Map<String, Object> resultMap = new HashMap<>();

        logger.print("text: " + text);

        // 创建一个画布 长, 宽都为 100 的 bufferedimage 对象
        BufferedImage image = new BufferedImage(800, 600, BufferedImage.TYPE_INT_BGR);
        Graphics2D graphics2D = image.createGraphics();

        // 采用线性渐变背景
        GradientPaint gp = new GradientPaint(0, 0, new Color(235, 242, 249), 0, image.getHeight(), new Color(255, 255, 255));
        graphics2D.setPaint(gp);
        graphics2D.fillRect(0, 0, image.getWidth(), image.getHeight());

        // 创建正则表达, 去除掉时间戳
        RegularExpression regularExpression = new RegularExpression();
        text = regularExpression.getTitleText(text);

        logger.print(text);

        // 设置随机背景颜色
        Random random = new Random();
        int r = random.nextInt(256);
        int g = random.nextInt(256);
        int b = random.nextInt(256);
        graphics2D.setColor(new Color(r, g, b));
        graphics2D.fillRect(0, 0, image.getWidth(), image.getHeight());

        // 设置字体 和 颜色
        graphics2D.setFont(new Font("微软雅黑", Font.PLAIN, 80));
        r = random.nextInt(256);
        g = random.nextInt(256);
        b = random.nextInt(256);
        graphics2D.setColor(new Color(r,g,b));

        // 设置标题位置
        int x = (image.getWidth() - graphics2D.getFontMetrics().stringWidth(text)) / 2;
        int y = random.nextInt(image.getHeight() / 4) + image.getHeight() / 3;
        //int y = (image.getHeight() - graphics2D.getFontMetrics().getHeight() + image.getHeight() / 10) / 2;

        // 绘制文字
        graphics2D.drawString(text, x, y);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", bos);
        byte[] bytes = bos.toByteArray();
        response.setContentType("image/jpeg");
        response.setContentLength(bytes.length);
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();

        // 释放资源
        graphics2D.dispose();
    }
}
